﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace kredit
{
    public class GameRules
    {
        public int fieldHeight;
        public int fieldWidth;
        public List<Robot> _playingRobots=new List<Robot>();
        private int _targetX;
        private int _targetY;

        private Random _random = new Random();

        private void PlaceTarget()
        {
            int x = 0;
            int y = 0;
            do
            {
                x = _random.Next(0, this.fieldWidth - 1);
                y = _random.Next(0, this.fieldHeight - 1);

            }
            
            while (IsPositionFree(x, y));
            _targetX = x;
            _targetY = y;
            
            
        }
        private Robot IsWinnerFound()
        {
            foreach (Robot r in this._playingRobots)
            {
                if (r._x == this._targetX && r._y == this._targetY)
                    return r;
            }
            return null;

        }


        public bool IsPositionFree(int x, int y)
        {
            return true;
        }

        public void StartGame(int fh, int fw, IEnumerable<Robot> robots)
        {
            this.fieldHeight = fh;
            this.fieldWidth = fw;
            foreach (Robot r in robots)
            {
                InitRobotPosition(r);
            }
            PlaceTarget();
            while (IsWinnerFound()==null)
            {
                foreach (Robot r in this._playingRobots)
                {
                    int currentRobotX = r._x;
                    int currentRobotY = r._y;
                    try
                    {
                        r.DoStep();
                        if (Math.Abs(r._x - currentRobotX) > 1  ||  Math.Abs(r._y - currentRobotY)>1)
                        {
                            r.SetPosition(currentRobotX, currentRobotY);
                        }
                    }
                    catch
                    {
                        r.SetPosition(currentRobotX, currentRobotY);
                    }
                    r.Print();
                }

            }
            Thread.Sleep(500);
        }
        private void InitRobotPosition(Robot r)
        {
            int x=0;
            int y=0;
            do
            {
            x = _random.Next(0, this.fieldWidth - 1);
            y = _random.Next(0, this.fieldHeight - 1);
        
            }
            while(IsPositionFree(x,y));
            r.SetPosition(x, y);
            this._playingRobots.Add(r);
        }

    }


    public class Robot
    {
        public int _x;
        public int _y;
        public int _lastPrintedX;
        public int _lastPrintedY;
        public bool _hasBeenPrinted;
        public int[] _hasBeenPosition;
        public ConsoleColor _color;
        
        public void DoStep()
        {
        }

        //public void DoStep(int interval, GameRules rules, Random rnd)
        //{
        //    int course = rnd.Next(0, 5);
        //    switch (course)
        //    {
        //        case 1: MoveLeft(interval, rules); break;
        //        case 2: MoveRight(interval, rules); break;
        //        case 3: MoveTop(interval, rules); break;
        //        case 4: MoveBottom(interval, rules); break;
        //    }
        //}
        public void MoveLeft(int interval)
        {

            _x -= interval;
            _x = Math.Max(_x, 0);
        }
        public void MoveRight(int interval)
        {
            _x += interval;
            _x = Math.Min(_x, 30 /*Console.BufferWidth - 1*/);
        }
        public void MoveTop(int interval)
        {
            _y -= interval;
            _y = Math.Max(_y, 0);
        }
        public void MoveBottom(int interval)
        {
            _y += interval;
            _y = Math.Min(_y, 30 /*Console.BufferWidth - 1*/);
        }
        public void HasBeenPosition()
        {
            //_hasBeenPosition[_x, _lastPrintedX] = 1;
        }

        public void Print()
        {
            if (_hasBeenPrinted)
            {
                //clear previous position
                Console.SetCursorPosition(_lastPrintedX, _lastPrintedY);
                Console.Write(" ");
            }

            Console.SetCursorPosition(_x, _y);
            Console.ForegroundColor = _color;
            Console.Write("#");
            _lastPrintedX = _x;
            _lastPrintedY = _y;
            _hasBeenPrinted = true;
        }

        public Robot(int x, int y, ConsoleColor color)
        {
            _x = x;
            _y = y;
            _color = color;

        }
        public void SetPosition(int x,int y)
        {
            
        }
       

    }
}

